package org.jeecg.modules.system.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.system.entity.CqProject;
import org.jeecg.modules.system.entity.CqProjectDetail;
import org.jeecg.modules.system.mapper.CqProjectDetailMapper;
import org.jeecg.modules.system.mapper.CqProjectMapper;
import org.jeecg.modules.system.service.ICqProjectService;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: priject
 * @Author: jeecg-boot
 * @Date:   2021-05-17
 * @Version: V1.0
 */
@Service
public class CqProjectServiceImpl extends ServiceImpl<CqProjectMapper, CqProject> implements ICqProjectService {

	@Autowired
	private CqProjectMapper cqProjectMapper;
	@Autowired
	private CqProjectDetailMapper cqProjectDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(CqProject cqProject, List<CqProjectDetail> cqProjectDetailList) {
		cqProjectMapper.insert(cqProject);
		if(cqProjectDetailList!=null && cqProjectDetailList.size()>0) {
			for(CqProjectDetail entity:cqProjectDetailList) {
				//外键设置
				entity.setProjectId(cqProject.getId());
				cqProjectDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(CqProject cqProject,List<CqProjectDetail> cqProjectDetailList) {
		cqProjectMapper.updateById(cqProject);
		
		//1.先删除子表数据
		cqProjectDetailMapper.deleteByMainId(cqProject.getId());
		
		//2.子表数据重新插入
		if(cqProjectDetailList!=null && cqProjectDetailList.size()>0) {
			for(CqProjectDetail entity:cqProjectDetailList) {
				//外键设置
				entity.setProjectId(cqProject.getId());
				cqProjectDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		cqProjectDetailMapper.deleteByMainId(id);
		cqProjectMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cqProjectDetailMapper.deleteByMainId(id.toString());
			cqProjectMapper.deleteById(id);
		}
	}
	
}
